const _ = require('underscore');
const Utils = {
    findView(pNode, mList, parentParams) {
        let childrenList = _.filter(mList, function (menu) {
            return menu.parent_id === pNode.menu_id;
        });
        if (childrenList.length) {
            pNode['children'] = [];
            let childrenGroup = _.groupBy(childrenList, 'menu_id');
            _.map(childrenGroup, function (childrens) {
                let nn = childrens[0];
                let menuNode = {menu_id: nn.menu_id, label: nn.label};
                if (nn.path) {
                    let firstView = _.find(childrens, {is_first: 1});
                    if (firstView) menuNode.jump = parentParams.path + '/' + firstView.path;
                    _.forEach(childrens, function (pathData) {
                        parentParams.viewList.push({
                            path: pathData.path,
                            url: parentParams.path + '/' + pathData.url
                        });
                    })
                } else {
                    Utils.findView(menuNode, mList, parentParams);
                }
                pNode['children'].push(menuNode);
            });
        }
    },
    getUserGroupAuth(menuRoutes) {
        let parentRoutesList = _.groupBy(_.filter(menuRoutes, {parent_id: 0}), 'menu_id');//在菜单关联路由列表中获取父级菜单以及相对应的路由，并按照menu_id进行分组
        let menuList = [];
        let routeList = [];
        _.forEach(parentRoutesList, function (parentGroup) {//循环父级菜单关联路由组
            let parentNode;
            let route;
            let isPathNode = _.filter(parentGroup, function (p) {
                return p.path;
            });
            if (isPathNode.length) {//如果节点的path字段不为空，说明此节点为叶子节点，直接对该节点添加
                let viewList = _.map(parentGroup, function (parentPath) {
                    if (parentPath.is_first) {
                        route = {path: parentPath.menu_path, children: []};
                        parentNode = {
                            menu_id: parentPath.menu_id,
                            label: parentPath.label,
                            jump: parentPath.menu_path + '/' + parentPath.path
                        }
                    }
                    return {
                        path: parentPath.path,
                        url: parentPath.menu_path + '/' + parentPath.url
                    }
                });
                route['children'] = viewList
            } else {
                let parent = parentGroup.shift();
                route = {path: parent.menu_path, children: []};
                let parentParams = {viewList: [], path: parent.menu_path};
                parentNode = {
                    menu_id: parent.menu_id, label: parent.label
                };
                Utils.findView(parentNode, menuRoutes, parentParams);
                route['children'] = parentParams.viewList;
            }
            menuList.push(parentNode);
            routeList.push(route);
        });
        return {menuList: JSON.stringify(menuList), routeList: JSON.stringify(routeList)};
    }
};
module.exports = Utils;